package com.coolgor.axnut.service.mdmis.impl;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.coolgor.axnut.dao.mdmis.PolicyStatisticsDao;
import com.coolgor.axnut.entity.mdmis.PolicyCondition;
import com.coolgor.axnut.service.mdmis.PolicyStatisticsService;

@Service
public class PolicyStatisticsServiceImpl implements PolicyStatisticsService {
	private static Logger log = LoggerFactory.getLogger(PolicyStatisticsServiceImpl.class);
	
	@Autowired
	private PolicyStatisticsDao policyStatisticsDao;	

	/**
	 * 根据销售项目 进行统计配送成功率
	 */
	@Override
	public List<Map<String,Object>> countReceiptRateByProject(PolicyCondition policyCondition) {
		List<Map<String,Object>> result = policyStatisticsDao.countReceiptRateByProject(policyCondition);
		//如果结果为空，直接返回null,并结束程序。
		if (result.isEmpty()) return null;
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(4);
		Float receiptRate;
		Float total;
		Float receipt;
		Map<String,Object> map;
		//汇总统计广西总量用的运算变量
		Float gx_total = 0F;
		Float gx_unintro = 0F;
		Float gx_intro = 0F;
		Float gx_send = 0F;
		Float gx_back = 0F;
		Float gx_backall = 0F;
		Float gx_receipt = 0F;
		Float gx_withdrawal = 0F;
		Float gx_surrender = 0F;
		for (int i=0; i<result.size(); i++) {
			// 逐个项目计算回销率
			map = result.get(i);
			receipt = Float.valueOf((String) map.get("receipt").toString());//必须toString才能转换成Float
			total = Float.valueOf(map.get("total").toString());//必须toString才能转换成Float
			if (total == 0) {
				receiptRate = 0F;
			} else {
				receiptRate = Float.valueOf(numberFormat.format(receipt/total));				
			}
			map.put("receipt_rate", receiptRate);
			result.set(i, map);
			// 逐个项目 累加到广西总单量
			gx_total += Float.valueOf((String) map.get("total").toString());
			gx_unintro += Float.valueOf((String) map.get("unintro").toString());
			gx_intro += Float.valueOf((String) map.get("intro").toString());
			gx_send += Float.valueOf((String) map.get("send").toString());
			gx_back += Float.valueOf((String) map.get("back").toString());
			gx_backall += Float.valueOf((String) map.get("backall").toString());
			gx_receipt += Float.valueOf((String) map.get("receipt").toString());
			gx_withdrawal += Float.valueOf((String) map.get("withdrawal").toString());
			gx_surrender += Float.valueOf((String) map.get("surrender").toString());
		}
		map = new HashMap<String,Object>();
		map.put("project", "广西汇总");
		map.put("total", gx_total);
		map.put("unintro", gx_unintro);
		map.put("intro", gx_intro);
		map.put("send", gx_send);
		map.put("back", gx_back);
		map.put("backall", gx_backall);
		map.put("receipt", gx_receipt);
		map.put("withdrawal", gx_withdrawal);
		map.put("surrender", gx_surrender);
		if (gx_total == 0) {
			map.put("receipt_rate", 0F);
		} else {
			map.put("receipt_rate", Float.valueOf(numberFormat.format(gx_receipt/gx_total)));				
		}
		result.add(map);
		return result;
	}

	/**
	 * 根据配送城市进行统计配送成功率
	 */
	@Override
	public List<Map<String,Object>> countReceiptRateByArea(PolicyCondition policyCondition) {
		List<Map<String,Object>> result = policyStatisticsDao.countReceiptRateByArea(policyCondition);
		//如果结果为空，直接返回null,并结束程序。
		if (result.isEmpty()) return null;
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(4);
		Float receiptRate;
		Float total;
		Float receipt;
		Map<String,Object> map;
		//汇总统计广西总量用的运算变量
		Float gx_total = 0F;
		Float gx_unintro = 0F;
		Float gx_intro = 0F;
		Float gx_send = 0F;
		Float gx_back = 0F;
		Float gx_backall = 0F;
		Float gx_receipt = 0F;
		Float gx_withdrawal = 0F;
		Float gx_surrender = 0F;
		for (int i=0; i<result.size(); i++) {
			// 逐个项目计算回销率
			map = result.get(i);
			receipt = Float.valueOf((String) map.get("receipt").toString());//必须toString才能转换成Float
			total = Float.valueOf(map.get("total").toString());//必须toString才能转换成Float
			if (total == 0) {
				receiptRate = 0F;
			} else {
				receiptRate = Float.valueOf(numberFormat.format(receipt/total));				
			}
			map.put("receipt_rate", receiptRate);
			result.set(i, map);
			// 逐个项目 累加到广西总单量
			gx_total += Float.valueOf((String) map.get("total").toString());
			gx_unintro += Float.valueOf((String) map.get("unintro").toString());
			gx_intro += Float.valueOf((String) map.get("intro").toString());
			gx_send += Float.valueOf((String) map.get("send").toString());
			gx_back += Float.valueOf((String) map.get("back").toString());
			gx_backall += Float.valueOf((String) map.get("backall").toString());
			gx_receipt += Float.valueOf((String) map.get("receipt").toString());
			gx_withdrawal += Float.valueOf((String) map.get("withdrawal").toString());
			gx_surrender += Float.valueOf((String) map.get("surrender").toString());
		}
		map = new HashMap<String,Object>();
		map.put("area", "广西汇总");
		map.put("total", gx_total);
		map.put("unintro", gx_unintro);
		map.put("intro", gx_intro);
		map.put("send", gx_send);
		map.put("back", gx_back);
		map.put("backall", gx_backall);
		map.put("receipt", gx_receipt);
		map.put("withdrawal", gx_withdrawal);
		map.put("surrender", gx_surrender);
		if (gx_total == 0) {
			map.put("receipt_rate", 0F);
		} else {
			map.put("receipt_rate", Float.valueOf(numberFormat.format(gx_receipt/gx_total)));				
		}
		result.add(map);
		return result;
	}
	
	/**
	 * 根据销售项目进行统计配送时效
	 */
	@Override
	public List<Map<String,Object>> countTimelinessByProject(PolicyCondition policyCondition) {
		List<Map<String,Object>> result = policyStatisticsDao.countTimelinessByProject(policyCondition);
		//如果结果为空，直接返回null,并结束程序。
		if (result.isEmpty()) return null;
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后4位
		numberFormat.setMaximumFractionDigits(4);
		Map<String,Object> map;
		//汇总统计广西总量用的运算变量
		Float gx_total = 0F;
		Float gx_receipt = 0F;
		Float gx_total_timeliness = 0F;
		Float gx_more3send = 0F;
		Float gx_more10day = 0F;
		for (int i=0; i<result.size(); i++) {
			map = result.get(i);
			// 遍历每一条记录，累加到广西总单量
			gx_total += Float.valueOf((String) map.get("total").toString());
			gx_receipt += Float.valueOf((String) map.get("receipt").toString());
			gx_total_timeliness += Float.valueOf((String) map.get("total_timeliness").toString());
			gx_more3send += Float.valueOf((String) map.get("more3send").toString());
			gx_more10day += Float.valueOf((String) map.get("more10day").toString());
		}
		map = new HashMap<String,Object>();
		map.put("project", "广西汇总");
		map.put("total", gx_total);
		map.put("receipt", gx_receipt);
		map.put("total_timeliness", gx_total_timeliness);
		map.put("more3send", gx_more3send);
		map.put("more10day", gx_more10day);
		if (gx_total == 0) {
			map.put("receipt_rate", 0F);
		} else {
			map.put("receipt_rate", Float.valueOf(numberFormat.format(gx_receipt/gx_total)));				
		}
		if (gx_receipt == 0) {
			map.put("timeliness", 0F);
		} else {
			map.put("timeliness", Float.valueOf(numberFormat.format(gx_total_timeliness/gx_receipt)));				
		}
		result.add(map);
		return result;
	}	
	
	/**
	 * 根据配送城市进行统计配送时效
	 */
	@Override
	public List<Map<String,Object>> countTimelinessByArea(PolicyCondition policyCondition) {
		List<Map<String,Object>> result = policyStatisticsDao.countTimelinessByArea(policyCondition);
		//如果结果为空，直接返回null,并结束程序。
		if (result.isEmpty()) return null;
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后4位
		numberFormat.setMaximumFractionDigits(4);
		Map<String,Object> map;
		//汇总统计广西总量用的运算变量
		Float gx_total = 0F;
		Float gx_receipt = 0F;
		Float gx_total_timeliness = 0F;
		Float gx_more3send = 0F;
		Float gx_more10day = 0F;
		for (int i=0; i<result.size(); i++) {
			map = result.get(i);
			// 遍历每一条记录，累加到广西总单量
			gx_total += Float.valueOf((String) map.get("total").toString());
			gx_receipt += Float.valueOf((String) map.get("receipt").toString());
			gx_total_timeliness += Float.valueOf((String) map.get("total_timeliness").toString());
			gx_more3send += Float.valueOf((String) map.get("more3send").toString());
			gx_more10day += Float.valueOf((String) map.get("more10day").toString());
		}
		map = new HashMap<String,Object>();
		map.put("area", "广西汇总");
		map.put("total", gx_total);
		map.put("receipt", gx_receipt);
		map.put("total_timeliness", gx_total_timeliness);
		map.put("more3send", gx_more3send);
		map.put("more10day", gx_more10day);
		if (gx_total == 0) {
			map.put("receipt_rate", 0F);
		} else {
			map.put("receipt_rate", Float.valueOf(numberFormat.format(gx_receipt/gx_total)));				
		}
		if (gx_receipt == 0) {
			map.put("timeliness", 0F);
		} else {
			map.put("timeliness", Float.valueOf(numberFormat.format(gx_total_timeliness/gx_receipt)));				
		}
		result.add(map);
		return result;
	}
	
	/**
	 * 根据接件日期，按月份统计，每个项目的送单量、回销量、成功率、时效
	 */
	@Override
	public List<Map<String,Object>> countByIntroMonth(PolicyCondition policyCondition) {
		List<Map<String,Object>> result = policyStatisticsDao.countByIntroMonth(policyCondition);
		//如果结果为空，直接返回null,并结束程序。
		if (result.isEmpty()) return null;
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后4位
		numberFormat.setMaximumFractionDigits(4);
		//汇总统计总量用的运算变量
		Float policy_count = 0F;
		Float receipt_count = 0F;
		Float timeliness = 0F;
		for (Map<String,Object> resultSet : result) {
			policy_count += Float.valueOf((String) resultSet.get("policy_count").toString());
			receipt_count += Float.valueOf((String) resultSet.get("receipt_count").toString());
			timeliness += Float.valueOf((String) resultSet.get("receipt_count").toString()) * Float.valueOf((String) resultSet.get("timeliness").toString());
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("month", "total");
		map.put("policy_count", policy_count);
		map.put("receipt_count", receipt_count);
		if (policy_count==0) {
			map.put("receipt_rate", 0);
		} else { 
			map.put("receipt_rate", Float.valueOf(numberFormat.format(receipt_count/policy_count)));	
		}
		if (receipt_count==0) { 
			map.put("timeliness", 0);
		} else {
			map.put("timeliness", Float.valueOf(numberFormat.format(timeliness/receipt_count)));
		}
		result.add(map);
		return result ;
	}
}
